$(function(){
	$("#main_tab").tabs();
	$("#create_rep,.rep_edit,#create_user,.user_set,.user_repo,#bu_rm,#bu_join").hover(function(){
		$(this).addClass("ui-state-hover");
	},function(){
		$(this).removeClass("ui-state-hover");
	});
	$("#create_rep").click(function(){
		$("[name='rep_name']").val("").removeAttr("disabled").removeClass("ui-state-error");
		$("[name='rep_context']").val("");
		$("[name='rep_anon']").val("none");
		$("[name='rep_auth']").val("none");
		$("#rep_name").dialog('destroy').dialog({
			resizable:false,
			title:'Create New Repository',
			bgiframe:true,
			autoOpen:false,
			height:320,
			width:430,
			modal:true,
			buttons:{Cancal:function(){
				$(this).dialog('close');
			},'Create Repository':function(){
				rep_name=$("[name='rep_name']");
				rep_context=$("[name='rep_context']");
				rep_anon=$("[name='rep_anon']");
				rep_auth=$("[name='rep_auth']");
				if(rep_name.val().length==0||rep_name.val().match(/^[a-zA-Z0-9_]+$/)==null){
					rep_name.addClass("ui-state-error");
				}else{
					rep_name.removeClass("ui-state-error");
					$('form',$('#rep_name')).submit();
				}
			}}
		}).dialog('open');
	});
	$('.rep_edit').click(function(){
		//$('#rep_name').dialog('open');
		//alert($(this).attr('name'))
		rep_name=$('td:nth-child(1)',$(this).parent().parent()).text();
		rep_context=$('td:nth-child(2)',$(this).parent().parent()).text();
		rep_anon=$('td:nth-child(4)',$(this).parent().parent()).text();
		rep_auth=$('td:nth-child(5)',$(this).parent().parent()).text();
		$("[name='rep_name']").val(rep_name).attr("disabled","disabled").removeClass("ui-state-error");
		$("[name='rep_context']").val(rep_context);
		$("[name='rep_anon']").val(rep_anon);
		$("[name='rep_auth']").val(rep_auth);
		$('#rep_name').dialog('destroy').dialog({
			resizable:false,
			title:'Edit Repository',
			bgiframe:true,
			autoOpen:false,
			height:320,
			width:430,
			modal:true,
			buttons:{Cancal:function(){
				$(this).dialog('close');
			},Save:function(){
				$("[name='rep_name']").removeAttr("disabled");
				$('form',$('#rep_name')).attr('action','?active=set_rep').submit();
				//alert("Save");
			},Delete:function(){
				c=confirm("Are you sure to DELETE this repository ?");
				if(c){
					$("[name='rep_name']").removeAttr("disabled");
					$('form',$('#rep_name')).attr('action','?active=del_rep').submit();
					//alert("delete");
				}
			}}
		}).dialog('open');
		$('.ui-dialog-buttonpane button:last').css('float','left');
	});
	$('#create_user').click(function(){
		$("[name='user_uid']").val('').removeAttr('disabled');
		$("[name='user_pwd']").val('');
		$("[name='user_pwd2']").val('');
		$("#add_user").dialog('destroy').dialog({
			resizable:false,
			title:'Create User',
			bgiframe:true,
			autoOpen:false,
			height:320,
			width:430,
			modal:true,
			buttons:{Cancal:function(){
				$(this).dialog('close');
			},"Create User":function(){
				user_uid=$("[name='user_uid']");
				user_pwd=$("[name='user_pwd']");
				user_pwd2=$("[name='user_pwd2']");
				
				user_uid.removeClass("ui-state-error");
				user_pwd.removeClass("ui-state-error");
				user_pwd2.removeClass("ui-state-error");
				if(user_pwd.val()!=user_pwd2.val()||user_pwd.val()==''){
					user_pwd.addClass("ui-state-error");
					user_pwd2.addClass("ui-state-error");
				}else if(user_uid.val()==''){
					user_uid.addClass("ui-state-error");
				}else{
					$('form',$('#add_user')).submit();
					//alert("add");
				}
			}}
		}).dialog('open');
	});
	$(".user_set").click(function(){
		user_name=$('td:nth-child(1)',$(this).parent().parent()).text();
		$("[name='user_uid']").val(user_name).attr("disabled","disabled").removeClass("ui-state-error");
		$("[name='user_pwd']").val('');
		$("[name='user_pwd2']").val('');
		$("#add_user").dialog('destroy').dialog({
			resizeable:false,
			title:'Setting User',
			bgiframe:true,
			autoOpen:false,
			height:320,
			width:430,
			modal:true,
			buttons:{Cancal:function(){
				$(this).dialog('close');
			},"Save":function(){
				user_uid=$("[name='user_uid']");
				user_pwd=$("[name='user_pwd']");
				user_pwd2=$("[name='user_pwd2']");
				if(user_pwd.val()!=user_pwd2.val()||user_pwd.val()==''){
					user_pwd.addClass("ui-state-error");
					user_pwd2.addClass("ui-state-error");
				
				}else{
					user_uid.removeAttr('disabled');
					$('form',$('#add_user')).attr('action','?active=set_user').submit();
					//alert("SAVE");
				}
			
			},'Delete User':function(){
				c=confirm("Are you sure to DELETE this user ?");
				if(c){
					$("[name='user_uid']").removeAttr('disabled');
					$('form',$('#add_user')).attr('action','?active=del_user').submit();
				}
			}}
		}).dialog('open');
		$('.ui-dialog-buttonpane button:last').css('float','left');
	});
	$(".user_repo").click(function(){
		user_name=$('td:nth-child(1)',$(this).parent().parent()).text();
		reps=$('td:nth-child(2)',$(this).parent().parent()).text();
		user_uid=$("[name='user_uid2']");
		all_repo=$("[name='all_rep']");
		rm_repo=$("[name='rm_repo']").empty();
		join_repo=$("[name='join_repo']").empty();
		
		user_uid.val(user_name);
		reps=reps.split(', ');
		//alert(reps);
		all_repo.children().each(function(){
			tmp=$(this).text()
			if($.inArray(tmp,reps)==-1){
				join_repo.append(new Option(tmp,tmp));
			}else{
				rm_repo.append(new Option(tmp,tmp));
			}
			//alert($(this).text());
		});
		
		$("#set_repo").dialog('destroy').dialog({
			resizeable:false,
			title:'Setting Repository',
			bgiframe:true,
			autoOpen:false,
			height:320,
			width:430,
			modal:true,
			buttons:{Cancal:function(){
				$(this).dialog('close');
			}}
		}).dialog('open');
	});
	$("#bu_rm").click(function(){
		if($("[name='rm_repo']").val()!=null){
			user_uid=$("[name='user_uid2']").removeAttr('disabled');
			$('form',$('#set_repo')).attr('action','?active=rm_user_repo').submit();
		}
	});
	$("#bu_join").click(function(){
		if($("[name='join_repo']").val()!=null){
			user_uid=$("[name='user_uid2']").removeAttr('disabled');
			$('form',$('#set_repo')).attr('action','?active=join_user_repo').submit();
		}
	});
});

